package com.dg11185.nearshop.activity;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.nearshop.R;
import com.dg11185.libs.network.NetClient;
import com.dg11185.nearshop.app.Constants;
import com.dg11185.nearshop.app.ViewUtils;
import com.umeng.analytics.MobclickAgent;

/**
 * 二级页面模板。包含一个返回按钮，并添加了返回事件。
 * 
 * @author wanliang527
 * 
 */
public class SubPageActivity extends BaseActivity {

    protected LinearLayout leftView, middleView, rightView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTopBanner();
    }

    /**
     * 添加返回按钮的操作在此进行，可以复写该方法实现标题栏的初始化
     */
    protected void initTopBanner() {
        leftView = getPartView(LEFT);
        middleView = getPartView(MIDDLE);
        rightView = getPartView(RIGHT);

        ImageButton backBtn = new ImageButton(this);
        backBtn.setId(1);

        backBtn.setBackgroundResource(R.drawable.drawable_tp2tl_gray);
        backBtn.setImageResource(R.drawable.ic_white_arrow_left_medium);

        backBtn.setOnClickListener(mClickListener);

        leftView.addView(backBtn, ViewUtils.WRAP_MATCH);

    }

    /**
     * 设置子页面标题。
     * 
     * @param title
     *            标题名
     */
    public void setSubPageTitle(String title) {
        if (middleView == null)
            return;

        TextView titleView = new TextView(this);
        titleView.setText(title);
        titleView.setTextColor(getResources().getColor(R.color.white));
        titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, Constants.TITLE_FONT_SIZE);
        titleView.setGravity(Gravity.CENTER);
        middleView.addView(titleView, ViewUtils.WRAP_MATCH);
    }

    private OnClickListener mClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case 1:
                    SubPageActivity.this.finish();
                    break;

                default:
                    break;
            }

        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getName());
        MobclickAgent.onResume(mActivity);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getName());
        MobclickAgent.onPause(mActivity);
    }

    @Override
    protected void onDestroy() {
        NetClient.httpCancel(this, true);
        super.onDestroy();
    }
}
